﻿(function () {
    $validate = window.$validate = function (options) {
        return new $validate.prototype.init(options);
    };
    $validate.prototype = {
        valilist: [
            { name: "isEmail", expression: /^\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w+$/, message: "邮箱格式不正确" },
            { name: "isInt", expression: /^[-+]?\d*$/, message: "必须为整数" },
            { name: "isDate", expression: /^(\d{4})\-(\d{2})\-(\d{2})$/, message: "日期格式不正确" },
            { name: "isDateShort", expression: /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/, message: "日期格式不正确" },
            { name: "isLetter", expression: /^[a-zA-Z]+$/, message: "必须为英文字母" },
            { name: "isTel", expression: /((d{3,4})|d{3,4}-)?d{7,8}(-d{3})*/, message: "电话格式不正确" },
            { name: "isAccount", expression: /^[a-zA-Z][a-zA-Z0-9_]{5,19}$/, message: "必须以字母开头，且是数字、字母、下划线的6-20位组合" },
            { name: "isUrl", expression: /a-zA-z]+:\/\/[^\s]*/, message: "网址格式不正确" },
            { name: "isIdcard", expression: /d{18}|d{15}/, message: "身份证号必须为15或18位数字" },
            { name: "isTel", expression: /d{6}/, message: "邮政编码必须为6位数字" },
            { name: "isNull", expression: /\S/, message: "此项不能为空" },
            { name: "isMobile", expression: /^[1][3][0-9]{9}$/, message: "手机号码不正确" },
            { name: "isChinease", expression: /^[\u0391-\uFFE5]+$/, message: "必须为中文" }
        ],
        extend: function EvUpdate(NewObject, OldObject) {
            function clonePrototype() { }
            clonePrototype.prototype = NewObject;
            var obj = new clonePrototype();
            for (var ele in obj) {
                if (typeof (obj[ele]) == "object")
                    EvUpdate(obj[ele], OldObject[ele]);
                else
                    OldObject[ele] = obj[ele];
            }
        },
        config: { doc: null, realtime: false, callback: null, uiback: null, tags: "input,select", valiattr: "data-validate-option", valimessageattr: "data-validate-message" },
        init: function (options) {
            this.extend(options, this.config);
            if (typeof (this.config.doc) == "string") {
                this.config.doc = document.getElementById(this.config.doc);
            }
            if (this.config.doc == null) {
                this.config.doc = document.body;
            }
            return this;
        },
        serch: function (name) {
            for (var i = 0; i < this.valilist.length; i++) {
                if (name == this.valilist[i].name) {
                    return this.valilist[i];
                }
            }
            return null;
        },
        begin: function (callui) {
            if (!this.config.doc) {
                return false;
            }
            if (this.config.doc.hasChildNodes()) {
                var alls = new Array();
                var tagarray = this.config.tags.split(',');
                for (var t = 0; t < tagarray.length; t++) {
                    alls.push(this.config.doc.getElementsByTagName(tagarray[t]));
                }
                for (var i = 0; i < alls.length; i++) {
                    for (var y = 0; y < alls[i].length; y++) {
                        var valitype = alls[i][y].getAttribute(this.config.valiattr);
                        var valione = this.serch(valitype);
                        if (valitype && valione) {
                            var value = alls[i][y].value;
                            var message = alls[i][y].getAttribute(this.config.valimessageattr);
                            var result = value.match(valione.expression);
                            if (result == null) {
                                if (callui == null) {
                                    this.show(alls[i][y], valione, message);
                                }
                                else {
                                    callui(alls[i][y], valione, message);
                                }
                                return false;
                            }
                        }
                    }
                }
            }
            else {
                var valitype = this.config.doc.getAttribute(this.config.valiattr);
                var valione = this.serch(valitype);
                if (valitype && valione) {
                    var value = this.config.doc.value;
                    var message = this.config.doc.getAttribute(this.config.valimessageattr);
                    var result = value.match(valione.expression);
                    if (result == null) {
                        if (callui == null) {
                            this.show(this.config.doc, valione, message);
                        }
                        else {
                            callui(this.config.doc, valione, message);
                        }
                        return false;
                    }
                }
            }
            return true;
        },
        show: function (domone, valione, message) {
            if (this.config.uiback == null) {
                domone.focus();
                var valimessage = message != null ? message : valione.message;
                var _hg = domone.clientHeight;
                var _width = domone.clientWidth;
                var t = domone.offsetTop;
                var l = domone.offsetLeft;
                while (domone = domone.offsetParent) {
                    t += domone.offsetTop;
                    l += domone.offsetLeft;
                };
                t = t + _hg;
                function parseNode(arg) { var objE = document.createElement("div"); objE.innerHTML = arg; return objE.childNodes[0]; };
                var str = "<div id=\"_lxpfloattip_\" onclick=\"javascript:document.body.removeChild(document.getElementById('_lxpfloattip_'))\" style=\"width:" + _width + "px;z-index: 800000; position: absolute; left: " + l + "px; top: " + t + "px;\"><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"width:200px\"><tbody><tr style=\"height:auto\"><td class=\"tip_lefttop\"></td><td class=\"tip_top\"></td><td class=\"tip_righttop\"></td></tr><tr><td class=\"tip_left\"></td><td id=\"_lxpfloattip_value\" bgcolor=\"#ffffda\" style=\"color:navy;font-size:14px\">" + valimessage + "</td><td class=\"tip_right\"></td></tr><tr><td class=\"tip_leftbottom\"></td><td class=\"tip_bottom\"></td><td class=\"tip_rightbottom\"></td></tr></tbody></table></div>";
                var validatedemo = parseNode(str);
                document.body.appendChild(validatedemo);
                setTimeout("document.body.removeChild(document.getElementById(\"_lxpfloattip_\"))", 2000);
            }
            else {
                domone.focus();
                this.config.uiback(domone, valione, message);
            }
        }
    };
    $validate.prototype.init.prototype = $validate.prototype;
})();